const { sequelize } = require('../utils/database');
const User = require('./User');
const Activity = require('./Activity');
const Feed = require('./Feed');
const Badge = require('./Badge');
const CheckIn = require('./CheckIn');
const UserProfile = require('./UserProfile');
const ActivityParticipant = require('./ActivityParticipant');
const UserFollow = require('./UserFollow');
const FeedLike = require('./FeedLike');
const FeedComment = require('./FeedComment');

// 用户关联
User.hasOne(UserProfile, { foreignKey: 'user_id', as: 'profile' });
UserProfile.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

User.hasMany(Activity, { foreignKey: 'created_by', as: 'createdActivities' });
Activity.belongsTo(User, { foreignKey: 'created_by', as: 'creator' });

User.hasMany(Feed, { foreignKey: 'user_id', as: 'feeds' });
Feed.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

User.hasMany(Badge, { foreignKey: 'user_id', as: 'badges' });
Badge.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

User.hasMany(CheckIn, { foreignKey: 'user_id', as: 'checkIns' });
CheckIn.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

// 活动关联
Activity.hasMany(Feed, { foreignKey: 'activity_id', as: 'feeds' });
Feed.belongsTo(Activity, { foreignKey: 'activity_id', as: 'activity' });

Activity.hasMany(Badge, { foreignKey: 'activity_id', as: 'badges' });
Badge.belongsTo(Activity, { foreignKey: 'activity_id', as: 'activity' });

Activity.hasMany(CheckIn, { foreignKey: 'activity_id', as: 'checkIns' });
CheckIn.belongsTo(Activity, { foreignKey: 'activity_id', as: 'activity' });

Activity.hasMany(ActivityParticipant, { foreignKey: 'activity_id', as: 'participants' });
ActivityParticipant.belongsTo(Activity, { foreignKey: 'activity_id', as: 'activity' });

// 用户活动参与关联
User.hasMany(ActivityParticipant, { foreignKey: 'user_id', as: 'participations' });
ActivityParticipant.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

// 多对多关联：用户参与的活动
User.belongsToMany(Activity, {
  through: ActivityParticipant,
  foreignKey: 'user_id',
  otherKey: 'activity_id',
  as: 'joinedActivities'
});

Activity.belongsToMany(User, {
  through: ActivityParticipant,
  foreignKey: 'activity_id',
  otherKey: 'user_id',
  as: 'joinedUsers'
});

// 用户关注关联
User.hasMany(UserFollow, { foreignKey: 'follower_id', as: 'following' });
User.hasMany(UserFollow, { foreignKey: 'following_id', as: 'followers' });
UserFollow.belongsTo(User, { foreignKey: 'follower_id', as: 'follower' });
UserFollow.belongsTo(User, { foreignKey: 'following_id', as: 'followingUser' });

// 多对多关联：用户关注关系
User.belongsToMany(User, {
  through: UserFollow,
  foreignKey: 'follower_id',
  otherKey: 'following_id',
  as: 'followingUsers'
});

User.belongsToMany(User, {
  through: UserFollow,
  foreignKey: 'following_id',
  otherKey: 'follower_id',
  as: 'followerUsers'
});

// 动态点赞关联
User.hasMany(FeedLike, { foreignKey: 'user_id', as: 'feedLikes' });
FeedLike.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

Feed.hasMany(FeedLike, { foreignKey: 'feed_id', as: 'likes' });
FeedLike.belongsTo(Feed, { foreignKey: 'feed_id', as: 'feed' });

// 动态评论关联
User.hasMany(FeedComment, { foreignKey: 'user_id', as: 'feedComments' });
FeedComment.belongsTo(User, { foreignKey: 'user_id', as: 'user' });

Feed.hasMany(FeedComment, { foreignKey: 'feed_id', as: 'comments' });
FeedComment.belongsTo(Feed, { foreignKey: 'feed_id', as: 'feed' });

// 评论自关联（回复功能）
FeedComment.hasMany(FeedComment, { foreignKey: 'parent_id', as: 'replies' });
FeedComment.belongsTo(FeedComment, { foreignKey: 'parent_id', as: 'parent' });

module.exports = {
  sequelize,
  User,
  Activity,
  Feed,
  Badge,
  CheckIn,
  UserProfile,
  ActivityParticipant,
  UserFollow,
  FeedLike,
  FeedComment
}; 